layui.config({
	base: '../../static/admin/js/module/'
}).extend({
	dialog: 'dialog',
	bootstrapTable:'bootstrap/bootstrap-table.min',
	BSTable:"bootstrap/bootstrap-table-object",
	BSTreeTable:"bootstrap/tree-table-object",
	bs_locales:"bootstrap/bootstrap-table-zh-CN",
	bootstrap:"bootstrap/bootstrap.min",
	jquery_ztree:"ztree/jquery.ztree.all.min",
	jquery_treegrid:"jquery-treegrid/js/jquery.treegrid.min",
	jquery_treegrid_bs3:"jquery-treegrid/js/jquery.treegrid.bootstrap3",
	jquery_treegrid_extension:"jquery-treegrid/extension/jquery.treegrid.extension",
});

layui.use(['form', 'jquery', 'laydate', 'layer', 'laypage', 'dialog','element'], function() {
		layer = layui.layer,
		$ = layui.jquery,
		dialog = layui.dialog;
	//获取当前iframe的name值
	var iframeObj = $(window.frameElement).attr('name');
	
	var checked = function(){
		var selected = $('table').bootstrapTable ? $('table').bootstrapTable('getSelections') : $('table').bootstrapTreeTable('getSelections');   
	    if(selected.length == 0){
	    	dialog.info("请先选中表格中的某一记录！");
	        return false;
	    }else{
	        layui.seItem = selected[0];
	        return true;
	    }
	}
	
	//顶部添加
	$('.addBtn').click(function() {
		var url=$(this).attr('data-url');
		//将iframeObj传递给父级窗口,执行操作完成刷新
		parent.page("菜单添加", url,bsTable, w = "700px", h = "620px");
		return false;

	}).mouseenter(function() {
		dialog.tips('添加', '.addBtn');
	})
	
	$(".editBtn").click(function(){
		if(checked()){
			var url=$(this).attr('data-url');
			var id = layui.seItem.id;
			url = url+"?id="+id;
			parent.page("菜单编辑", url,bsTable, w = "700px", h = "620px");
			return false;	
		}
		return false;
	}).mouseenter(function() {
		dialog.tips('编辑', '.editBtn');
	})
	
	var delFunc = function(url,id){
		dialog.confirm({
			message:'您确定要删除选中项',
			success:function(){
				$.post(url,{id:id},function(data){
					data && layer.msg('删除成功');
					//刷新表格
					data && bsTable.refresh();
					!data && layer.msg("删除失败");
				});
			},
			cancel:function(){
				layer.msg('取消了')
			}
		})
	}
		//顶部批量删除
	$('.delBtn').click(function() {
		var url=$(this).attr('data-url');
		if(checked()){
			var id = layui.seItem.id;
			delFunc(url,id);
		}
		return false;
	}).mouseenter(function() {
		dialog.tips('删除', '.delBtn');
	})
	//时间选择
	var laydate = layui.laydate;
	$(".layer-date").each(function(){
		$(this).click(function(){
			var istime = $(this).attr("istime");
			var format = $(this).attr("format");
			laydate({elem:this,istime:istime,format:format});
		});
	});

	//顶部排序
	$('.listOrderBtn').click(function() {
		var url=$(this).attr('data-url');
		dialog.confirm({
			message:'您确定要进行排序吗？',
			success:function(){
				layer.msg('确定了')
			},
			cancel:function(){
				layer.msg('取消了')
			}
		})
		return false;

	}).mouseenter(function() {

		dialog.tips('批量排序', '.listOrderBtn');

	})	

	//列表添加
	$('#table-list').on('click', '.add-btn', function() {
		var url=$(this).attr('data-url');
		//将iframeObj传递给父级窗口
		parent.page("菜单添加", url, iframeObj, w = "700px", h = "620px");
		return false;
	})
	//列表删除
	$('#table-list').on('click', '.del-btn', function() {
		var url=$(this).attr('data-url');
		var id = $(this).attr('data-id');
		delFunc(url,id);
		return false;
	})
	//列表跳转
	$('#table-list,.tool-btn').on('click', '.go-btn', function() {
		var url=$(this).attr('data-url');
		var id = $(this).attr('data-id');
		window.location.href=url+"?id="+id;
		return false;
	})
	//编辑栏目
	$('#table-list').on('click', '.edit-btn', function() {
		var That = $(this);
		var id = That.attr('data-id');
		var url=That.attr('data-url');
		//将iframeObj传递给父级窗口
		parent.page("菜单编辑", url + "?id=" + id, iframeObj, w = "700px", h = "620px");
		return false;
	})
});

/**
 * 控制iframe窗口的刷新操作
 */
var bootstrapTable;
var bsTable=null;
//父级弹出页面
function page(title, url,obj, w, h) {
	if(title == null || title == '') {
		title = false;
	};
	if(url == null || url == '') {
		url = "404.html";
	};
	if(w == null || w == '') {
		w = '700px';
	};
	if(h == null || h == '') {
		h = '350px';
	};
	bootstrapTable = obj;
	//如果手机端，全屏显示
	if(window.innerWidth <= 768) {
		var index = layer.open({
			type: 2,
			title: title,
			area: [320, h],
			fixed: false, //不固定
			content: url
		});
		layer.full(index);
	} else {
		var index = layer.open({
			type: 2,
			title: title,
			area: [w, h],
			fixed: false, //不固定
			content: url
		});
	}
}

/**
 * 刷新子页,关闭弹窗
 */
function refresh() {
	//根据传递的name值，获取子iframe窗口，执行刷新
//	if(window.frames[iframeObjName]) {
//		window.frames[iframeObjName].location.reload();
//
//	} else {
//		window.location.reload();
//	}
	bootstrapTable.refresh();
	layer.closeAll();
}

var TreeTable = function(data){
	
	layui.use(['BSTreeTable','form'],function(){
		bsTable = new layui.BSTreeTable(data.id,data.url,data.data);
		bsTable.setExpandColumn(2);
		bsTable.setIdField(data.idField);
		bsTable.setCodeField(data.codeField);
		bsTable.setParentCodeField(data.parentCodeField);
		bsTable.setExpandAll(data.expandAll);
		bsTable.init();
		 var form = layui.form();
		 form.on('submit(search)', function(res) {
			 if(data.searchData!=undefined){
				 res = data.searchData(res);
			 }
			 bsTable.refresh({query: res.field});
			 return false;
		});
		 
	});
}
var Table= function(data){
	layui.use(['BSTable','form'],function(){
		bsTable = new layui.BSTable(data.id,data.url,data.data);
		data.type && bsTable.setPaginationType(data.type);
		data.search && bsTable.setSearch(data.search);
		bsTable.init();
		 var form = layui.form();
		 form.on('submit(search)', function(res) {
			 if(data.searchData!=undefined){
				 res = data.searchData(res);
			 }
			 bsTable.refresh({query: res.field});
			 return false;
		});
	});
}
